//
//  LDMicroCompanyCellTableViewCell.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/7/25.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit




class LDMicroCompanyCellTableViewCell: LDBaseRecFinderCell {
    
    let margin_V = LDMargin_V //竖直间隔
    let BtnHeight:CGFloat = 15
    
    //上部
    var jobTitle = UILabel()
    var salaryLab = UILabel()
    
    var cityBtn = UIButton()
    var experienceBtn = UIButton()
    var eduBtn = UIButton()
    
    var divideLineView1 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    
    //中部
    
    var jobDescTitle:UILabel?
    var jobDesc:UILabel?

    
    var divideLineView2 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    //下部
    var detailsBtn:UIButton!
    
    var models = DDCompanyInfoJobModel() {
        didSet{
            
            updateContentSub()
        }
    }
    
    var tapMoreDescBlock:((cell:LDMicroCompanyCellTableViewCell)->())?
    //frame
    private let btnMargin:CGFloat = 10

    //MARK: - OVERRIDE
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        //布局
        setDefaultContent()
        setupFrame()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func updateContentSub(){
        
        let publishModel:DDCompanyInfoJobModel = self.models
        
        //上
        jobTitle.text = publishModel.jobtitle ?? "职位"
        jobTitle.font = UIFont.systemFontOfSize(13)
        salaryLab.text = "¥\(publishModel.salary)" ?? "薪资"
        salaryLab.font = UIFont.systemFontOfSize(13)

        
        cityBtn.setTitle(publishModel.city ?? "城市", forState: .Normal)
        experienceBtn.setTitle(publishModel.experience ?? "经验", forState: .Normal)
        eduBtn.setTitle(publishModel.edu ?? "学历", forState: .Normal)
        //中
        
        let attr = String.getAttrFormString(lineH: 8, text: publishModel.jobdesc ?? "职位描述", font: LDFont.Font12)
        jobDesc?.attributedText = attr
        
        jobDesc!.snp_updateConstraints(closure: { (make) in
            
            make.height.equalTo(models.jobDescH)
        })
        
        updateFrames()
    }
    
    func updateFrames() {
        

            let textsize = CGSizeMake(120, 13)
            
            let width1 = (models.city ?? "城市").boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
            let width2 = (models.experience ?? "经验").boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
            let width3 = (models.edu ?? "教育").boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font12], context: nil).size.width
        
                
                
                cityBtn.snp_updateConstraints { (make) in
                    
                    make.width.equalTo(width1 + 30)
                }
                
                experienceBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width2 + 30)
                    
                }
                
                eduBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width3 + 30)
                }
        
            updateConstraintsIfNeeded()
        
    }
    
    
    
    
    // MARK: - init 布局
    //添加view到主视图
    func setDefaultContent(){
        
        
        jobTitle = UILabel.setHeadTitleLab("Title")
        salaryLab = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.Red,Alignment: nil)
        
        
        cityBtn = UIButton.setLoactionBtn()
        experienceBtn = UIButton.setExperienceBtn()
        eduBtn = UIButton.setEduBtn()
        
        //分割线--------
        jobDescTitle = UILabel.setDetailsLab("职位描述", fontSize: 13, titleColor: LDGolbalColor.GreenAssist, Alignment: .Left)
        jobDesc = UILabel()
        jobDesc?.numberOfLines = 0
        
        //分割线--------
        
        let btn = UIButton.setDetailsBtn("展开", picName: "more_detail_btn", btnBackColor: nil, titleColor: LDFontColor.Green)
        btn?.setImage(UIImage(named: "more_detail_btn_up"), forState: UIControlState.Selected)
        btn?.setTitle("收起", forState: UIControlState.Selected)
        
        detailsBtn = UIButton.setButtonImageToRight(btn!)
        
        
        detailsBtn.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext { [weak self] (obj) in
            
            
            if self == nil {
                return
            }
            
            self!.detailsBtn.selected = !self!.detailsBtn.selected
            if self?.tapMoreDescBlock != nil {
                
                self?.tapMoreDescBlock!(cell: self!)
            }
        }
        

        contentView.addSubview(jobTitle)
        contentView.addSubview(salaryLab)
        contentView.addSubview(divideLineView1)
        contentView.addSubview(cityBtn)
        contentView.addSubview(experienceBtn)
        contentView.addSubview(eduBtn)
        
        contentView.addSubview(jobDescTitle!)
        contentView.addSubview(jobDesc!)
        
        contentView.addSubview(divideLineView2)
        addSubview(detailsBtn)

    }
    
    //MARK - 自动布局
    func setupFrame(){
        
        //头部
        jobTitle.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(20)
            make.left.equalTo(15)
            make.height.equalTo(20)
        })
        
        salaryLab.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(jobTitle)
            make.right.equalTo(-margin_V*4)
            make.height.equalTo(20)
        })
        
        cityBtn.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(jobTitle.snp_bottom).offset(LDMargin_V*1.5)
            make.height.equalTo(12)
            make.left.equalTo(jobTitle)
        })
        
        experienceBtn.snp_makeConstraints(closure: { (make) in
            
            make.height.equalTo(12)
            make.top.equalTo(cityBtn)
            make.left.equalTo(cityBtn.snp_right).offset(margin_V)
            
        })
        
        eduBtn.snp_makeConstraints(closure: { (make) in
            
            make.height.equalTo(12)
            make.top.equalTo(cityBtn)
            make.left.equalTo(experienceBtn.snp_right).offset(margin_V)
        })
        
        //分割线
        divideLineView1.snp_makeConstraints { (make) in
            
            make.top.equalTo(cityBtn.snp_bottom).offset(margin_V*1.5)
            make.height.equalTo(1)
            make.left.equalTo(LDMargin_H*3)
            make.right.equalTo(-LDMargin_H*3)
        }
        
        //中间
        jobDescTitle?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(divideLineView1.snp_bottom).offset(margin_V*1.5)
            make.left.equalTo(jobTitle)
            make.height.equalTo(15)
            
        })
        
        jobDesc?.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(jobDescTitle!.snp_bottom).offset(margin_V*1.5)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(5)
//            make.bottom.equalTo(divideLineView2.snp_top)

        })
       
        //分割线
        divideLineView2.snp_makeConstraints { (make) in
            
            make.top.equalTo(jobDesc!.snp_bottom).offset(margin_V)
            make.height.equalTo(1)
            make.left.equalTo(LDMargin_H*3)
            make.right.equalTo(-LDMargin_H*3)
        }
        
        //下部
        detailsBtn.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(divideLineView2.snp_bottom)
            make.left.right.equalTo(0)
            make.height.equalTo(35)

        })
        
    }
}


